package chis.fx.stage;

import java.awt.Point;
import java.util.Map;
import java.util.TreeMap;


public class Code {

	// Constant data members.
	private final double SCENE_WIDTH = 800;
	private final double SCENE_HEIGHT = 600;
	private final double SCENE_INSET_TOP = 10;
	private final double SCENE_INSET_RIGHT = 10;
	private final double SCENE_INSET_BOTTOM = 10;
	private final double SCENE_INSET_LEFT = 10;
	private final double HUD_WIDTH_PERCENT = 1;
	private final double HUD_HEIGHT_PERCENT = .30;
	private final double GAMECANVAS_WIDTH_PERCENT = 1;
	private final double GAMECANVAS_HEIGHT_PERCENT = .70;

	// Variable data members.
	
	// Layout dimensions.
	private double adjustedSceneWidth;
	private double adjustedSceneHeight;
	private double HUDWidth;
	private double HUDHeight;
	private double gameCanvasWidth;
	private double gameCanvasHeight;

	// Circle values.
	private double circleWidth;
	private double circleRadius;	
	private int numOfCirclesAcross;
	private double numOfCirclesDown;
	private double totalNumOfCircles;
	
	// Collection of clicked circle coordinates.
	private Map<Integer,MyPoint> circleCoordinates;
	
	private void initialiseMyStage() {
		circleCoordinates = new TreeMap<Integer,MyPoint>();
		layoutStage();
	}
	
	private void layoutStage() {
		adjustSceneDimensions();
		calculateHUDDimensions();
		calculateGameCanvasDimensions();
		makeHUD();
		makeGameCanvas();
	}
	
	private void adjustSceneDimensions() {
		adjustedSceneWidth = SCENE_WIDTH - (SCENE_INSET_LEFT + SCENE_INSET_RIGHT);
		adjustedSceneHeight = SCENE_HEIGHT - (SCENE_INSET_TOP + SCENE_INSET_BOTTOM);
	}
	
	private void calculateHUDDimensions() {
		HUDWidth = HUD_WIDTH_PERCENT * adjustedSceneWidth;
		HUDHeight = HUD_HEIGHT_PERCENT * adjustedSceneHeight;
	}
	
	private void calculateGameCanvasDimensions() {
		gameCanvasWidth = GAMECANVAS_WIDTH_PERCENT * adjustedSceneWidth;
		gameCanvasHeight = GAMECANVAS_HEIGHT_PERCENT * adjustedSceneHeight;
	}
	
	private void makeHUD() {
		
	}

	
	
	private void makeGameCanvas() {
		
		initialiseCircleCollectionDimensions();

		// circle click
		// addClickedCircle(...
	}
	
	private void initialiseCircleCollectionDimensions() {
		circleWidth = gameCanvasWidth / numOfCirclesAcross;
		circleRadius = circleWidth / 2;
		numOfCirclesDown = gameCanvasHeight / circleWidth;
		totalNumOfCircles = numOfCirclesAcross * numOfCirclesDown;
	}
	
	private void addClickedCircle(int centerX, int centerY) {
		MyPoint p = getTopLeftCircleCoordinate(centerX,centerY);
		int position = getPositionInGrid(p);
		circleCoordinates.put(position, p);
	}

	public MyPoint getTopLeftCircleCoordinate(int centerX, int centerY) {
		MyPoint p = new MyPoint();
		p.setX(centerX - circleRadius);
		p.setY(centerY - circleRadius);
		return p;
	}

	public int getPositionInGrid(MyPoint circle) {
		int position;
		int positionInRow = (int) (circle.getX() + 1);
		int rowNumber = (int) (circle.getY() + 1);
		int balanceOfRow = numOfCirclesAcross - positionInRow;
		position = (rowNumber * numOfCirclesAcross) - balanceOfRow;
		return position;
	}

}
